﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using BLL;
using Model;
using Common;
using AdminWeb.Filter;
using AdminWeb.Models;

namespace AdminWeb.Controllers
{
    [Area("Admin")]
    public class BlogController : Controller
    {
        [CheckAdminLogin]
        public ActionResult List(int page=1)
        {
            BlogBLL bll = new BlogBLL();
            string search_url = "";
            string extsql = "";
            int pageCount = 10;
            string title_search = Request.Query["title_search"];
            if(!string.IsNullOrEmpty(title_search))
            {
                search_url = "&title_search=" + title_search;
                extsql += " and blog_title like '%"+ title_search + "%'";
            }
            ViewBag.mess = bll.GetPage(extsql,page, pageCount);
            ViewBag.title_search = title_search;
            ViewBag.search_url = search_url;
            ViewBag.request_url = Request.Path.ToString();
            ViewBag.pageCount = pageCount;
            ViewBag.admin = BaseOperater.getAdmin(HttpContext);
            return View();
        }

        [CheckAdminLoginAjax]
        public string Delete()
        {
            BlogBLL bll = new BlogBLL();
            return JSONHelper.SerializeObject(bll.delete(Utils.StrToInt(Request.Query["id"],0)));
        }

        [CheckAdminLogin]
        public ActionResult Edit(string id)
        {
            TagBLL tagbll = new TagBLL();
            CategoryBLL catebll = new CategoryBLL();
            BlogBLL bll = new BlogBLL();
            ViewBag.TagList = tagbll.GetList();
            ViewBag.CateList = catebll.GetList();
            ViewBag.model = bll.GetModel(id);
            return View();
        }
        [CheckAdminLogin]
        [HttpGet]
        public ActionResult Add()
        {
            TagBLL tagbll = new TagBLL();
            CategoryBLL catebll = new CategoryBLL();
            ViewBag.TagList = tagbll.GetList();
            ViewBag.CateList = catebll.GetList();
            return View();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult BlogAdd()
        {
            Member admin = BaseOperater.getAdmin(HttpContext);
            BlogBLL bll = new BlogBLL();
            Blog model = new Blog();
            model.blog_tags= Request.Form["tag"];
            model.blog_cate = Request.Form["cate"];
            model.blog_title = Request.Form["title"];
            model.blog_subtitle = Request.Form["subtitle"];
            model.blog_content = Request.Form["content"];
            model.blog_adminid = admin.mem_id;
            model.blog_addtime = DateTime.Now;
            model.blog_delete = 0;
            model.blog_type= Utils.StrToInt(Request.Form["type"], 0);
            return Json(bll.Add(model));
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult Save()
        {
            Member admin = BaseOperater.getAdmin(HttpContext);
            BlogBLL bll = new BlogBLL();
            Blog model = new Blog();
            model.blog_id =Utils.StrToInt(Request.Form["blogid"],0);
            model.blog_tags = Request.Form["tag"];
            model.blog_cate = Request.Form["cate"];
            model.blog_title = Request.Form["title"];
            model.blog_subtitle = Request.Form["subtitle"];
            model.blog_content = Request.Form["content"];
            model.blog_type = Utils.StrToInt(Request.Form["type"], 0);
            return Json(bll.Update(model));
        }

        /// <summary>
        /// 生成所有文件
        /// </summary>
        /// <returns></returns>
        [CheckAdminLoginAjax]
        public JsonResult CreateAll()
        {
            BlogBLL bll = new BlogBLL();
            return Json(bll.CreateAll());
        }

    }
}